<?php
/**
 * PhpUnderControl_ApiAdmin_Test
 *
 * 针对 ./v1/Api/Admin.php Api_Admin 类的PHPUnit单元测试
 *
 * @author: dogstar 20160322
 */

require_once dirname(__FILE__) . '/../test_env.php';

if (!class_exists('Api_Admin')) {
    require dirname(__FILE__) . '/./v1/Api/Admin.php';
}

class PhpUnderControl_ApiAdmin_Test extends PHPUnit_Framework_TestCase
{
    public $apiAdmin;
    protected $token;

    protected function setUp()
    {
        parent::setUp();

        $this->apiAdmin = new Api_Admin();
    }

    protected function tearDown()
    {
    }


    /**
     * @group testGetRules
     */ 
    public function testGetRules()
    {
        $rs = $this->apiAdmin->getRules();
    }

    /**
     * @group testLogin
     */ 
    public function testLogin()
    {
        //Step 1. 构建请求URL
        $url = 'service=Admin.Login&username=socialpark&password=thinkpark';

        //Step 2. 执行请求
        $rs = PhalApi_Helper_TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertArrayHasKey('token', $rs);
        $this->assertNotEmpty('token', $rs['token']);
        $this->assertNotEmpty('token', $rs['info']);
        $this->assertArrayHasKey('info', $rs);
        $this->assertEquals('socialpark', $rs['info']['username']);

        $this->token=$rs['token'];

//        $rs = $this->apiAdmin->login();
//
//        $this->assertTrue(is_int($rs));

    }


    /**
     * @group testLists
     */ 
    public function testLists()
    {

        //Step 1. 构建请求URL
        $url = 'service=Admin.List&token='.$this->token;

        //Step 2. 执行请求
        $rs = PhalApi_Helper_TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertArrayHasKey('data', $rs);



    }

    /**
     * @group testInfo
     */ 
    public function testInfo()
    {

        //Step 1. 构建请求URL
        $url = 'service=Admin.List&token='.$this->token."&userid=1";

        //Step 2. 执行请求
        $rs = PhalApi_Helper_TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertArrayHasKey('username', $rs);



    }

    /**
     * @group testAdd
     */ 
    public function testAdd()
    {
        //Step 1. 构建请求URL
//        $url = 'service=Admin.Add&token='.$this->token."&userid=1";

        //Step 2. 执行请求
//        $rs = PhalApi_Helper_TestRunner::go($url);

        //Step 3. 验证
//        $this->assertNotEmpty($rs);
//        $this->assertArrayHasKey('username', $rs);


    }

    /**
     * @group testUpdate
     */ 
    public function testUpdate()
    {

    }

    /**
     * @group testDelete
     */ 
    public function testDelete()
    {


    }

    /**
     * @group testLogout
     */
    public function testLogout()
    {

        //Step 1. 构建请求URL
        $url = 'service=Admin.Logout&token='.$this->token;

        //Step 2. 执行请求
        $rs = PhalApi_Helper_TestRunner::go($url);

        //Step 3. 验证
        $this->assertNotEmpty($rs);
        $this->assertArrayHasKey('islogout', $rs);

    }
}
